#include "MaterialGraphPinView.h"

#include "MaterialGraphPinViewModel.h"
#include "Widgets/TextWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Mouse/MouseWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

void RMaterialGraphPinView::BindViewModel(TSharedObjectPtr<RMaterialGraphPinViewModel> InModel)
{
    BaseViewModel=InModel;
}

void RMaterialGraphPinView::CreateWidget(
    TSharedObjectPtr<RWidget> InNodeWidget
    ,TSharedObjectPtr<RWidget> InParent
    )
{
    auto Pin=NewWidget<RRectangleWidget>(InParent);
    Pin->Width= REditor::Get()->CurrentTheme->PinSize;
    Pin->Height=REditor::Get()->CurrentTheme->PinSize;
    Pin->Radius=REditor::Get()->CurrentTheme->PinSize/2;
    Pin->FillColor=BaseViewModel->GetPinColor();
    Pin->Anchor.VerticalCenterReference=InNodeWidget;
    Pin->Anchor.VerticalCenterReferenceEdge=EVerticalWidgetEdge::Top;
    Pin->Anchor.VerticalCenterOffset=[this](){
        return BaseViewModel->GetCenterYInNodeWidget();
    };
    Pin->Anchor.HorizontalCenterReference=InNodeWidget;
    Pin->Anchor.HorizontalCenterReferenceEdge=EHorizontalWidgetEdge::Left;
    Pin->Anchor.HorizontalCenterOffset=[this](){
        return BaseViewModel->GetCenterXInNodeWidget();
    };
    
    Pin->BorderSize=REditor::Get()->CurrentTheme->PinBorderSize;
    Pin->BorderColor=REditor::Get()->CurrentTheme->PinBorderColor;

    Pin->Visible=[this](){
        return !BaseViewModel->IsHidden();
    };

    auto PinMouse=NewWidget<RMouseWidget>(Pin);
    PinMouse->Anchor.Fill(Pin);
    PinMouse->Drag->bIsEnabled=true;
    PinMouse->Drag->OnDragStarted.Bind([this](){
        BaseViewModel->OnDragStarted();
    });
    PinMouse->Drag->OnDragEnded.Bind([this](){
        BaseViewModel->OnDragEnded();
    });

}

SVec4 RMaterialGraphPinView::GetDefaultXYZW() const
{
    return BaseViewModel->GetDefaultXYZW();
}

bool RMaterialGraphPinView::HasValidLink() const
{
    return BaseViewModel->HasValidLink();
}
